/*
 *  $Id: Abstract.cs 1239 2011-03-23 12:51:24Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;
using System.IO;

using Nereal.Serialization;

namespace Ragima.Events {
    /// <summary>
    /// Абстрактный класс игрового события, возможного для передачи по сети
    /// </summary>
    [XmlRootName("event"), TypeResolver(typeof(AttrTypeResolver<AbstractEvent>), "type")]
    public abstract class AbstractEvent {
        public static readonly Serializer EventSerializer;
        private static readonly ITypeIdDictionary EventDictionary;

        static AbstractEvent() {
            EventSerializer = new Serializer { InsertXmlDeclaration = false, IndentOutput = false };
            Ragima.Serialization.Resolvers.Register(EventSerializer);
            EventDictionary = SerializationConfig.ResolverManager.GetTypeIdDictionary(typeof(AbstractEvent));
        }

        public bool NeedSend {
            get { return EventDictionary.ContainsType(GetType()); }
        }

        private bool _remote = false;

        [NotSerialize]
        public bool Remote {
            get { return _remote; }
            set { _remote = value; }
        }

        public static AbstractEvent Load(Stream input) {
            return EventSerializer.Deserialize<AbstractEvent>(input, false);
        }

        public void Save(Stream output) {
            EventSerializer.Serialize(this, output, false);
        }

        public abstract void Execute(OldScene scene);

        public void WriteToConsole(string prefix) {
            if (!NeedSend)
                return;
            System.Console.WriteLine("{0} event: remote={1}", prefix, Remote);
            EventSerializer.Serialize(this, System.Console.Out, false);
            System.Console.WriteLine();
        }
    }
}
